package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: heima-leadnews
 * @description:
 * @author: lydms
 * @create: 2024-04-11 10:24
 **/

@Service
@Slf4j
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper,WmSensitive> implements WmSensitiveService {
    /**
     * 添加敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(WmSensitive adSensitive) {
        // 1.判断参数是否为空
        if (adSensitive == null || adSensitive.getSensitives() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 2.判断敏感词是否重复
        WmSensitive sensitive = this.getOne(new QueryWrapper<WmSensitive>().eq("sensitives", adSensitive.getSensitives()));
        if (sensitive != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 3.添加敏感词
        this.save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 修改敏感词
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult pageList(ChannelDto sensitiveDto) {
        // 1.判断参数是否为空
        if (sensitiveDto == null ) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        sensitiveDto.checkParam();

        IPage page = new Page(sensitiveDto.getPage(), sensitiveDto.getSize());
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        //模糊查询
        if (sensitiveDto.getName() != null) {
            queryWrapper.like(WmSensitive::getSensitives, sensitiveDto.getName());
        }
        IPage<WmSensitive> sensitivePage = this.page(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), (int) sensitivePage.getTotal());
        responseResult.setData(sensitivePage.getRecords());

        return responseResult;

    }

    /**
     * 修改敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive adSensitive) {

        // 1.判断参数是否为空
        if (adSensitive == null || adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //修改敏感词
        this.updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        // 1.判断参数是否为空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 2.删除敏感词
        this.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
